/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package provider.impl.genomemap.model;

import commons.core.Generator;
import commons.provider.ProviderException;
import commons.provider.impl.BaseProviderImpl;
import genomemap.model.KSA00;
import genomemap.provider.Providers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 *
 *
 * @version 1.3.0
 * @author Susanta Tewari
 * @history Created on 1/7/13
 * @since 1.3.0
 */
public class PImpl_Gen_KSA00_Shuffle extends BaseProviderImpl<Generator.MODEL<KSA00, KSA00>>
        implements Providers.KSA00GeneratorProvider {

    /**
     * Exhaustive probe orders for a given model.
     */
    @Override
    public Generator.MODEL<KSA00, KSA00> create() throws ProviderException {

        return new Generator.MODEL<KSA00, KSA00>() {

            @Override
            public Iterable<KSA00> generate(final KSA00 input, final int genCount) {

                final List<String> input_probes = new ArrayList<>(input.getProbes());

                return new Iterable<KSA00>() {

                    @Override
                    public Iterator<KSA00> iterator() {
                        return algo(input, genCount);
                    }
                };
            }
        };
    }

    private static Iterator<KSA00> algo(final KSA00 input, final int genCount) {

        final List<String> input_probes = new ArrayList<>(input.getProbes());

        return new Iterator<KSA00>() {

            int count                         = 0;
            private final RuntimeException ue = new UnsupportedOperationException();
            @Override
            public boolean hasNext() {
                return count < genCount;
            }
            @Override
            public KSA00 next() {

                final KSA00 ksa00 = new KSA00(input_probes, input.getProbeSpacing(),
                                              input.getChromosomeLength(), input.getCloneLength(),
                                              input.getFalsePositiveProbability(),
                                              input.getFalseNegativeProbability());

                Collections.shuffle(input_probes);

                count++;

                return ksa00;
            }
            @Override
            public void remove() {
                throw ue;
            }
        };
    }

    @Override
    public Class<KSA00> getModelClass() {
        return KSA00.class;
    }
}
